'''
@author: pnuzhnyi
'''

from django.db import models
from django.utils import simplejson as json
from lib.utils import Property

'''
    ----------------------------------------------------------------------------------
    Surfer stats
    ----------------------------------------------------------------------------------
    "level": 5, 
    "strength": 13, 
    "stamina": 20,
    "fame": 40, 
    "cash": 15000
'''

class Surfer(models.Model):
    fbook_id = models.CharField(max_length=20)
    stats_dump = models.CharField(max_length=100)
    
    @Property
    def stats():
        def fget(self):
            return json.loads(self.stats_dump)

        def fset(self, value):
            self.stats_dump = json.dumps(value)

        def fdel(self):
            pass

        return locals()
    
    @staticmethod
    def toJSON(i):
        r=None
        if type(i).__name__== "list":
            r = []
            for item in i:
                r.append({"guid":item.id,"stats":json.loads(item.stats)})
        else:
            r =   {"guid":i.id,"stats":json.loads(i.stats)}
        return json.dumps(r)